package com.sheep.jucdemo.part7.part7_4;

import java.util.Random;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * @Date 2022/4/27 21:50
 * @Created by sheep
 */
public class Test7_4_1 {

    static class Task implements Comparable<Task>{

        private int priority = 0;

        private String taskName;

        public int getPriority() {
            return priority;
        }

        public void setPriority(int priority) {
            this.priority = priority;
        }

        public String getTaskName() {
            return taskName;
        }

        public void setTaskName(String taskName) {
            this.taskName = taskName;
        }

        @Override
        public int compareTo(Task o) {
            if (this.priority >= o.priority){
                return 1;
            }else {
                return -1;
            }
        }

        public void doSomeThing(){
            System.out.println(taskName + ":" + priority);
        }
    }

    public static void main(String[] args) {
        // 创建任务，并添加到队列
        PriorityBlockingQueue<Task> priorityQueue = new PriorityBlockingQueue<>();
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            Task task = new Task();
            task.setPriority(random.nextInt(50));
            task.setTaskName("taskName" + i);
            priorityQueue.offer(task);
        }

        while (!priorityQueue.isEmpty()){
            Task task = priorityQueue.poll();
            if (null != task){
                task.doSomeThing();
            }
        }

    }
}
